<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use App\Services\Helper;

class XdLoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

   use AuthenticatesUsers;
   
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }

    /**
     * 用户退出方法
     */
    public function logout()
    {
        session()->pull('username', 'default');
        return Helper::jsonSuccess('退出成功');
    }

    // 登录 
    public function login(Request $request)
    {
        if($request->isMethod('post')) {
            $username = $request->input('username');
            $password = $request->input('password');                
            if (Auth::attempt(['username' => $username, 'password' => $password])) {
                $uid = Auth::user()['uid'];  
                session(['uid' => $uid]);
                return Helper::jsonSuccess('登录成功');
            } else {
                return Helper::jsonError('用户名或密码错误');
            }
        }
    }


}
